<script setup>
import {delDept, getDept, listCategory, updateDept} from '@/api/system/dept.js'

const {proxy} = getCurrentInstance()
const {sys_normal_disable} = proxy.useDict('sys_normal_disable')

const dataList = ref([])
const loading = ref(false)
const total = ref(0)
const ids = ref([])
const open = ref(false)
const title = ref('')
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
})

const {queryParams, form, rules} = toRefs(data)

function getList() {
  loading.value = true
  listCategory(queryParams.value).then(response => {
    dataList.value = response.data
    total.value = response.data.total
    loading.value = false
  })
}

getList()

function handleQuery() {
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

function reset() {
  form.value = {
    deptName: '',
    status: '0'
  }
  proxy.resetForm('formRef')
}

function handleAdd(row) {
  reset()
  // 接口().then(response => {
  //
  // })
}

function handleUpdate(row) {
  reset()
  getDept(row.deptId).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改社团种类'
  })
}

function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除名称为"' + row.deptName + '"的数据项?').then(function () {
    return delDept(row.deptId)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  }).catch(() => {
  })
}

function submitForm() {
  updateDept(form.value).then(response => {
    proxy.$modal.msgSuccess('修改成功')
    open.value = false
    getList()
  })
}

function cancel() {
  open.value = false
  reset()
}
</script>

<template>


  <div class="app-container">

    <el-form ref="queryRef" :inline="true" :model="queryParams">
      <el-form-item label="社团种类名称" prop="deptName">
        <el-input
            v-model="queryParams.deptName"
            clearable
            placeholder="请输入社团名称"
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="社团状态" style="width: 200px">
          <el-option
              v-for="dict in sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="Plus"
                   plain
                   type="primary"
                   @click="handleAdd">新增
        </el-button>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>

      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55px"/>
      <el-table-column align="center" label="编号" prop="deptId" width="100px"/>
      <el-table-column :show-overflow-tooltip="true" align="center" label="社团种类名称" prop="deptName" width="200"/>
      <el-table-column align="center" label="创建时间" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" align="center" label="更新时间" prop="updateTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="状态" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>


      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="180">
        <template #default="scope">
          <el-button v-hasPermi="['system:dept:edit']" icon="Edit" link type="primary" @click="handleUpdate(scope.row)">
            修改
          </el-button>
          <el-button v-if="scope.row.parentId !== 0" v-hasPermi="['system:dept:remove']" icon="Delete" link
                     type="primary"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="600px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="社团种类名称" prop="dept_name">
              <el-input v-model="form.deptName" maxlength="30" placeholder="请输入社团种类名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                    v-for="dict in sys_normal_disable"
                    :key="dict.value"
                    :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<style lang="scss" scoped>

</style>
